package com.jf.identity.task;

import com.jf.identity.entity.*;
import com.jf.identity.task.service.VerifyInfoTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 定时处理考生验证信息
 *
 * @author FCG
 */
@Component
public class VerifyInfoTask {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private VerifyInfoTaskService verifyInfoTaskService;

    /**
     * 储存考生验证信息到数据库：
     * 1.验证轨迹 jf_vis_db_20181009.i_ksyzgj
     * 2.验证信息 jf_vis_db_20181009.i_ksyzxx
     * 3.验证统计信息 jf_exdas_db.g_i_statistic_validation (电子地图用)
     */
    //@Scheduled(fixedRate = 5000)
    public void setExamineeVerifyInfos() {
        long start = System.currentTimeMillis();
        //一次处理1000个接口数据
        Query query = new Query().limit(1000);
        List<VerifyInfoFeedBack> verifyInfoFeedBacks
                = mongoTemplate.find(query, VerifyInfoFeedBack.class);
        if (verifyInfoFeedBacks == null || verifyInfoFeedBacks.isEmpty()) {
            return;
        }
        int info = 0;
        int trace = 0;
        int staticV = 0;
        int enterS = 0;
        //数据库操作成功，从mongoDB删除数据。
        //已经成功存储到数据库的考试验证信息
        List<VerifyInfoFeedBack> verifyInfoFeedBackInserted = new ArrayList<>(verifyInfoFeedBacks.size());
        for (VerifyInfoFeedBack verifyInfoFeedBack : verifyInfoFeedBacks) {
            try {
                VerifyInfoTaskCount verifyInfoTaskCount = verifyInfoTaskService.setVerifyInfo(verifyInfoFeedBack);
                //上传考生验证信息
                info += verifyInfoTaskCount.getInfo();
                //上传考生验证轨迹
                trace += verifyInfoTaskCount.getTrace();
                //上传验证总计信息
                staticV += verifyInfoTaskCount.getStaticV();
                //上传考生入场信息
                enterS += verifyInfoTaskCount.getEnterS();
                verifyInfoFeedBackInserted.add(verifyInfoFeedBack);
            } catch (Exception e) {
                logger.error("定时上传考生信息失败", e);
            }
        }
        for (VerifyInfoFeedBack verifyInfoFeedBack : verifyInfoFeedBackInserted) {
            mongoTemplate.remove(verifyInfoFeedBack);
        }
        long end = System.currentTimeMillis();
        logger.debug("\r\n取出数据:{} 条,\r\n上传考生验证信息:{} 条,\r\n上传考生验证轨迹:{} 条," +
                        "\r\n上传电子地图统计数据:{} 条,\r\n上传考生入场数据:{} 条," +
                        "\r\n总耗时:{} millis",
                verifyInfoFeedBacks.size(), info, trace, staticV, enterS, (end - start));
    }


}

















